﻿// =====================================================    
// Author: hexid26
// Email : hexid26@outlook.com
// Date  : 2015/07/06
// This code is for sharing
// Copyright (C)

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Threading;


namespace ToukidenKiwamiHacker
{
    public partial class Form1 : Form
    {
        public delegate void FlushClient();//代理
        Thread thread;
        private System.IntPtr processAddr = IntPtr.Zero;
        private long baseAddr;                              // 游戏常规数据的基地址
        private long baseAddrBattle;                        // 游戏战斗数据的基地址
        private string theProcessName;                      // 主进程名称
        private DataDefine dataDef = new DataDefine();      // 游戏道具的代码
        private static int weaponCNT = 0;                   // 武器计数器
        private static int armorCNT = 0;                    // 防具计数器
        private static int itemCNT = 0;                     // 道具计数器
        private static int btnMitamaCNT = 0;

        System.Windows.Forms.Timer timer = new System.Windows.Forms.Timer();

        public Form1()
        {
            InitializeComponent();
            buttonsDisable(1);

            comboBoxMitama.Items.AddRange(dataDef.mitamaList);
            comboBoxMitama.SelectedIndex = dataDef.mitamaList.Length - 1;
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            timer.Interval = 500;
            timer.Tick += delegate(object o, EventArgs args)
            {
                DoWork();
            };
            timer.Start();
        }

        private void DoWork()
        {
            timer.Interval = 1500;
            int res = checkGameVersion();
            switch (res)
            {
                case 0:
                    buttonsDisable(1);
                    break;
                case 1:
                    buttonsEnable(1);
                    break;
                case 2:
                    break;
                default:
                    break;
            }
            //checkGameVersion();
            //System.Threading.Thread.Sleep(3000);
        }

        //读取制定内存中的值
        public long ReadMemoryValue(long baseAdd)
        {
            return Helper.ReadMemoryValue(baseAdd, theProcessName);
        }

        //将值写入指定内存中
        public void WriteMemory(long baseAdd, int value)
        {
            Helper.WriteMemoryValue(baseAdd, theProcessName, value);
        }

        private void buttonsEnable(int flag)
        {
            btnBoxMax.Visible = true;
            btnBoxReduce.Visible = true;
            btnAddAllItem.Visible = true;
            btnAddAllMitama.Visible = true;
            btnAddWeapon.Visible = true;
            btnDelWeapon.Visible = true;
            btnAddArmor.Visible = true;
            btnDelArmor.Visible = true;
            btnAddOneItem.Visible = true;
            btnUnlockWeaponSpecialist.Visible = true;
            btnDelAllWeapon.Visible = true;
            btnDelAllArmor.Visible = true;
            btnAddOneMitama.Visible = true;
            btnAllItemGet.Visible = true;
            btnModifyMitamaOnWeapon.Visible = true;
            btnGetMitamaOnWeapon.Visible = true;
            btnResetRegister.Visible = true;
            if (flag == 1)
            {
                btnMitamaOnBattle1.Visible = false;
                btnMitamaOnBattle2.Visible = false;
                btnMitamaOnBattle3.Visible = false;
                btnMitamaOnBattle4.Visible = false;
                btnMitamaOnBattle5.Visible = false;
                btnMitamaOnBattle6.Visible = false;
                btnMitamaOnBattle7.Visible = false;
                btnDelAllMitata.Visible = true;
            }
            this.Update();
        }

        private void buttonsDisable(int flag)
        {
            btnBoxMax.Visible = false;
            btnBoxReduce.Visible = false;
            btnAddAllItem.Visible = false;
            btnAddAllMitama.Visible = false;
            btnAddWeapon.Visible = false;
            btnDelWeapon.Visible = false;
            btnAddArmor.Visible = false;
            btnDelArmor.Visible = false;
            btnAddOneItem.Visible = false;
            btnUnlockWeaponSpecialist.Visible = false;
            btnDelAllWeapon.Visible = false;
            btnDelAllArmor.Visible = false;
            btnAddOneMitama.Visible = false;
            btnAllItemGet.Visible = false;
            btnModifyMitamaOnWeapon.Visible = false;
            btnGetMitamaOnWeapon.Visible = false;
            btnResetRegister.Visible = false;
            if (flag == 1)
            {
                btnDelAllMitata.Visible = false;
                btnMitamaOnBattle1.Visible = false;
                btnMitamaOnBattle2.Visible = false;
                btnMitamaOnBattle3.Visible = false;
                btnMitamaOnBattle4.Visible = false;
                btnMitamaOnBattle5.Visible = false;
                btnMitamaOnBattle6.Visible = false;
                btnMitamaOnBattle7.Visible = false;
            }
            this.Update();
        }

        public int checkGameVersion()
        {
            string langVersion = "";
            theProcessName = "";
            string str = "";

            // 在进程列表中查找包含指定字符串的进程
            Process[] processes;
            processes = System.Diagnostics.Process.GetProcesses();
            foreach (Process p in processes)
            {
                str = p.ProcessName;
                if (str.Length < 17)
                {
                    continue;
                }
                if (str.Substring(0, 14) == "ToukidenKiwami")
                {
                    theProcessName = str;
                    langVersion = str.Substring(15, 2);
                    processAddr = p.MainModule.BaseAddress;
                    break;
                }
            }
            if (langVersion == "")
            {
                labelInfo.Text = "游戏没有运行";
                labelInfo.Update();
                langVersion = "";
                baseAddr = 0;
                baseAddrBattle = 0;
                buttonsDisable(1);
                return 0;
            }
            else if (langVersion == "en")
            {
                // 计算基础地址
                if (baseAddr == 0)
                {
                    baseAddr = ReadMemoryValue((long)processAddr + 0x78FF58);
                    baseAddrBattle = ReadMemoryValue((long)processAddr + 0x78FEF8);
                    buttonsEnable(1);
                    labelInfo.Text = "已经运行英文版游戏\r\n" + baseAddr.ToString("x8").ToUpper() + "  " + baseAddrBattle.ToString("x8").ToUpper();
                    return 1;
                }
                if (baseAddr == ReadMemoryValue((long)processAddr + 0x78FF58))
                {
                    return 2;
                }
                else
                {
                    baseAddr = 0;
                    return 0;
                }
            }
            else if (langVersion == "ja")
            {
                // 计算基础地址
                if (baseAddr == 0)
                {
                    baseAddr = ReadMemoryValue((long)processAddr + 0x78E078);
                    baseAddrBattle = ReadMemoryValue((long)processAddr + 0x78E018);
                    buttonsEnable(1);
                    labelInfo.Text = "已经运行日文版游戏\r\n" + baseAddr.ToString("x8").ToUpper() + "  " + baseAddrBattle.ToString("x8").ToUpper();
                    return 1;
                }
                if (baseAddr == ReadMemoryValue((long)processAddr + 0x78E078))
                {
                    return 2;
                }
                else
                {
                    baseAddr = 0;
                    return 0;
                }
            }
            return 0;
        }

        private void boxMax()           // 仓库容量全满
        {
            long addrItemBox = baseAddr + 0xA104;
            long addrWeaponBox = baseAddr + 0xEA78;
            long addrArmorBox = baseAddr + 0x11574;

            WriteMemory(addrItemBox, dataDef.itemBoxMax);
            WriteMemory(addrWeaponBox, dataDef.weaponBoxMax);
            WriteMemory(addrArmorBox, dataDef.armorBoxMax);

            addrItemBox = 0;
            addrWeaponBox = 0;
            addrArmorBox = 0;
        }

        private int[] boxReduce(int sub)        // 缩小仓库容量
        {
            long addrItemBox = baseAddr + 0xA104;
            long addrWeaponBox = baseAddr + 0xEA78;
            long addrArmorBox = baseAddr + 0x11574;

            int[] currentSize = new int[] { 0, 0, 0 };
            currentSize[0] = (int)ReadMemoryValue(addrItemBox) > sub ? (int)ReadMemoryValue(addrItemBox) - sub : 99;
            currentSize[1] = (int)ReadMemoryValue(addrWeaponBox) > sub ? (int)ReadMemoryValue(addrWeaponBox) - sub : 99;
            currentSize[2] = (int)ReadMemoryValue(addrArmorBox) > sub ? (int)ReadMemoryValue(addrArmorBox) - sub : 99;

            WriteMemory(addrItemBox, currentSize[0]);
            WriteMemory(addrWeaponBox, currentSize[1]);
            WriteMemory(addrArmorBox, currentSize[2]);

            addrItemBox = 0;
            addrWeaponBox = 0;
            addrArmorBox = 0;
            return currentSize;
        }

        private void addAllItemGet()            // 全素材获得判定
        {
            long addrGet = baseAddr + 0x954C;

            for (int i = dataDef.materialMinID; i <= dataDef.materialMaxID; i++)
            {
                // 铁匠铺记录刷新，解决武器无法升级
                WriteMemory(addrGet, 3);
                addrGet += 2;
            }
            addrGet = 0;
        }

        private void addAllItem()
        {
            long addrItem = baseAddr + 0x7F6C;
            long addrItemBox = baseAddr + 0xA104;

            // 修改道具箱上限
            WriteMemory(addrItemBox, dataDef.itemBoxMax);

            for (int i = dataDef.materialMinID; i <= dataDef.materialMaxID; i++)
            {
                // 写入2比特
                if (i < dataDef.materialID.Length)
                {
                    WriteMemory(addrItem, dataDef.materialID[i]);
                    WriteMemory(addrItem + 2, 78);
                }
                else
                {
                    WriteMemory(addrItem, 0xffff);
                    WriteMemory(addrItem + 2, 0);
                }
                addrItem += 4;
            }
            addAllItemGet();
            addrItem = 0;
            addrItemBox = 0;
            itemCNT = dataDef.mitamaList.Length;
        }

        private void addOneItem(int itemID)         // 添加单个道具
        {
            long addrItem = baseAddr + 0x7F6C;
            long addrGet = baseAddr + 0x954C;

            if ((int)ReadMemoryValue(addrItem) != 0xffff)
            {
                if (((int)ReadMemoryValue(addrItem + itemCNT * 4) == 0xffff) && ((int)ReadMemoryValue(addrItem + itemCNT * 4 - 4) != 0xffff))
                {
                    addrItem = addrItem + itemCNT * 4;
                }
                else
                {
                    itemCNT = 0;
                    buttonsDisable(0);
                    labelInfo.Text = "正在努力修改中，请稍候。。。";
                    labelInfo.Update();
                    while ((int)ReadMemoryValue(addrItem) != 0xffff)
                    {
                        addrItem += 4;
                        itemCNT++;
                    }
                    buttonsEnable(0);
                }
            }
            else
            {
                itemCNT = 0;
            }

            if (itemCNT > 1385)
            {
                MessageBox.Show("仓库已被占用1385格，小心爆仓");
            }
            WriteMemory(addrItem, itemID);
            WriteMemory(addrItem + 2, 78);
            itemCNT++;

            WriteMemory(addrGet + itemID * 2, 3);
            addrItem = 0;
            addrGet = 0;
        }

        private void delAllMitama()
        {
            //仅仅保留 047 |  源頼光 -- Minamoto no Yorimitsu
            long addrMitama = baseAddr + 0x11578;
            for (int i = dataDef.mitamaMinID; i < 400; i++)
            {
                if (i != 47)
                {
                    WriteMemory(addrMitama, 0);
                    WriteMemory(addrMitama + 4, 0);
                    WriteMemory(addrMitama + 8, 0xffff);
                    WriteMemory(addrMitama + 10, 0xffff);
                    WriteMemory(addrMitama + 12, 0x00);
                }
                else
                {
                    WriteMemory(addrMitama, 0);
                    WriteMemory(addrMitama + 4, 0x070b);
                    WriteMemory(addrMitama + 8, 0x0100);
                    WriteMemory(addrMitama + 10, 0x0b02);
                    WriteMemory(addrMitama + 12, 0x0b);
                }
                addrMitama += 16;
            }
        }

        private void addMitamaRange(int startIndex, int sum)
        {
            long addrMitama = baseAddr + 0x11578 + startIndex * 16;
            for (int i = 0; (i < sum) && (startIndex + i) <= 400; i++)
            {
                if (startIndex + i <= dataDef.mitamaMaxID)
                {
                    int flagGet = (int)(ReadMemoryValue(addrMitama + 4) & 0xff00);      // 0 没得到，非 0 已有
                    int level = (int)(ReadMemoryValue(addrMitama + 4) & 0xff);          // 游戏等级要 +1, 0B 是满级
                    if (flagGet == 0)       // 没有得到的魂
                    {
                        WriteMemory(addrMitama, 0);
                        WriteMemory(addrMitama + 4, 0x070b);
                        WriteMemory(addrMitama + 8, 0x0100);
                        WriteMemory(addrMitama + 10, 0x0b02);
                        WriteMemory(addrMitama + 12, 0x0b);
                        addrMitama += 16;
                        continue;
                    }
                    else if (level < 3)     // 等级小于3，技能用默认初始技能
                    {
                        WriteMemory(addrMitama + 4, flagGet + 0x0b);
                        WriteMemory(addrMitama + 8, 0x0100);
                        WriteMemory(addrMitama + 10, 0x0b02);
                        WriteMemory(addrMitama + 12, 0x0b);
                        addrMitama += 16;
                        continue;
                    }
                    else                    // 等级大于3，不该动技能，只改等级
                    {
                        WriteMemory(addrMitama + 4, flagGet + 0x0b);
                        //大于3级的御魂不该写技能
                        //WriteMemory(addrMitama + 8, 0x0100);
                        //WriteMemory(addrMitama + 10, 0xff02);
                        WriteMemory(addrMitama + 11, 0x0b0b);
                        addrMitama += 16;
                        continue;
                    }

                }
                else                        // 暂时没有开放的御魂栏位
                {
                    WriteMemory(addrMitama, 0);
                    WriteMemory(addrMitama + 4, 0);
                    WriteMemory(addrMitama + 8, 0xffff);
                    WriteMemory(addrMitama + 10, 0xffff);
                    WriteMemory(addrMitama + 12, 0x00);
                }
                addrMitama += 16;
            }

            addrMitama = 0;
        }

        private void modifyMitamaOnWeapon(int id1, int id2, int id3)
        {
            long addrWeapon = baseAddr + 0xA108;
            long addrEquipedIndex = baseAddr + 0x7708;

            int equipedIndex = (int)(ReadMemoryValue(addrEquipedIndex) & 0xffff);
            for (int i = 0; i < equipedIndex; i++)
            {
                addrWeapon += 16;
            }
            WriteMemory(addrWeapon + 2, id1);
            WriteMemory(addrWeapon + 4, id2);
            WriteMemory(addrWeapon + 6, id3);
            addrWeapon = 0;
            addrEquipedIndex = 0;
        }

        private void addOneWeapon(int weaponID)
        {
            long addrWeapon = baseAddr + 0xA108;
            long addrWeaponGet = baseAddr + 0xD308;

            if (ReadMemoryValue(addrWeapon) != -1)
            {
                if ((ReadMemoryValue(addrWeapon + weaponCNT * 16) == -1) && (ReadMemoryValue(addrWeapon + weaponCNT * 16 - 16) != -1))
                {
                    addrWeapon = addrWeapon + weaponCNT * 16;
                }
                else
                {
                    weaponCNT = 0;
                    buttonsDisable(0);
                    while (ReadMemoryValue(addrWeapon) != -1)
                    {
                        addrWeapon += 16;
                        weaponCNT++;
                    }
                    buttonsEnable(0);
                }
            }
            else
            {
                weaponCNT = 0;
            }

            if (weaponCNT > 770)
            {
                MessageBox.Show("武器数量已经超过770。请不要爆仓！");
            }

            WriteMemory(addrWeapon, weaponID);
            WriteMemory(addrWeapon + 10, 9);
            WriteMemory(addrWeaponGet + 3 * weaponID, 0x11);

            addrWeapon = 0;
            addrWeaponGet = 0;
            weaponCNT++;
        }

        private int delLastWeapon()
        {
            long addrWeapon = baseAddr + 0xA108;

            if (ReadMemoryValue(addrWeapon) != -1)
            {
                if ((ReadMemoryValue(addrWeapon + weaponCNT * 16) == -1) && (ReadMemoryValue(addrWeapon + weaponCNT * 16 - 16) != -1))
                {
                    addrWeapon = addrWeapon + weaponCNT * 16;
                }
                else
                {
                    weaponCNT = 0;
                    buttonsDisable(0);
                    while (ReadMemoryValue(addrWeapon) != -1)
                    {
                        addrWeapon += 16;
                        weaponCNT++;
                    }
                    buttonsEnable(0);
                }
            }
            else
            {
                weaponCNT = 0;
            }

            addrWeapon -= 16;
            long res = (long)ReadMemoryValue(addrWeapon) & 0xffff;
            WriteMemory(addrWeapon, 0xffff);
            WriteMemory(addrWeapon + 2, 0xffff);
            WriteMemory(addrWeapon + 4, 0xffff);
            WriteMemory(addrWeapon + 6, 0xffff);
            WriteMemory(addrWeapon + 8, 0);
            WriteMemory(addrWeapon + 10, 0);
            WriteMemory(addrWeapon + 12, 0);
            WriteMemory(addrWeapon + 14, 0);
            weaponCNT--;
            addrWeapon = 0;
            return (int)res;
        }

        private void delAllWeapon()
        {
            long addrEquipedIndex = baseAddr + 0x7708;
            long addrWeapon = baseAddr + 0xA108;

            WriteMemory(addrEquipedIndex, 0);
            addrWeapon += 16;
            for (int i = 1; i <= dataDef.weaponBoxMax; i++)
            {
                if (ReadMemoryValue(addrWeapon) != -1)
                {
                    WriteMemory(addrWeapon, 0xffff);
                    WriteMemory(addrWeapon + 2, 0xffff);
                    WriteMemory(addrWeapon + 4, 0xffff);
                    WriteMemory(addrWeapon + 6, 0xffff);
                    WriteMemory(addrWeapon + 8, 0);
                    WriteMemory(addrWeapon + 10, 0);
                    WriteMemory(addrWeapon + 12, 0);
                    WriteMemory(addrWeapon + 14, 0);
                    addrWeapon += 16;
                }
                else
                {
                    break;
                }
            }
            weaponCNT = 0;
        }

        private void addArmor(int armorID, int type)         // 这里的 type 是自定义的 0 1 2 3 表示单装备，4表示套装
        {
            long addrArmor = baseAddr + 0xEA7C;
            long addrArmorGet = baseAddr + 0x109BC;

            if (ReadMemoryValue(addrArmor) != 0x4ffff)
            {
                if ((ReadMemoryValue(addrArmor + armorCNT * 10) == 0x4ffff) && (ReadMemoryValue(addrArmor + armorCNT * 10 - 10) != 0x4ffff))
                {
                    addrArmor = addrArmor + armorCNT * 10;
                }
                else
                {
                    armorCNT = 0;
                    buttonsDisable(0);
                    while (ReadMemoryValue(addrArmor) != 0x4ffff)
                    {
                        addrArmor += 10;
                        armorCNT++;
                    }
                    buttonsEnable(0);
                }
            }
            else
            {
                armorCNT = 0;
            }


            switch (type)
            {
                case 0:
                    // 添加单独的头盔 0
                    WriteMemory(addrArmor, armorID);
                    WriteMemory(addrArmor + 2, 0x900);
                    WriteMemory(addrArmorGet + 3 * (armorID * 4), 0x11);
                    armorCNT++;
                    break;
                case 1:
                    // 添加单独的服饰 1
                    WriteMemory(addrArmor, armorID);
                    WriteMemory(addrArmor + 2, 0x901);

                    for (int cnt = 0; cnt < 4; cnt++)
                    {
                        if (cnt == 1)
                        {
                            WriteMemory(addrArmorGet + 3 * (armorID * 4 + cnt), 0x31);
                        }
                        else
                        {
                            WriteMemory(addrArmorGet + 3 * (armorID * 4 + cnt), 0x20);
                        }
                    }
                    armorCNT++;
                    break;
                case 2:
                    // 添加单独的护手 2
                    WriteMemory(addrArmor, armorID);
                    WriteMemory(addrArmor + 2, 0x902);
                    WriteMemory(addrArmorGet + 3 * (armorID * 4 + 2), 0x11);
                    armorCNT++;
                    break;
                case 3:
                    // 添加单独的鞋 3
                    WriteMemory(addrArmor, armorID);
                    WriteMemory(addrArmor + 2, 0x903);
                    WriteMemory(addrArmorGet + 3 * (armorID * 4 + 3), 0x11);
                    armorCNT++;
                    break;
                case 4:
                    // 普通套装
                    for (int cnt = 0; cnt < 4; cnt++)
                    {
                        WriteMemory(addrArmor, armorID);
                        WriteMemory(addrArmor + 2, cnt + 0x900);
                        addrArmor += 10;
                        WriteMemory(addrArmorGet + 3 * (armorID * 4 + cnt), 0x11);
                    }
                    addrArmor = 0;
                    addrArmorGet = 0;
                    armorCNT += 4;
                    break;
                default:
                    break;
            }
            if (armorCNT > 770)
            {
                MessageBox.Show("防具已经超过770件了，小心爆棚！");
            }
        }

        private void delAllArmor()
        {
            long addrEquiped = baseAddr + 0x7700;
            long addrArmor = baseAddr + 0xEA7C;

            WriteMemory(addrEquiped, dataDef.armorBoxMax);
            WriteMemory(addrEquiped + 2, dataDef.armorBoxMax);
            WriteMemory(addrEquiped + 4, dataDef.armorBoxMax);
            WriteMemory(addrEquiped + 6, dataDef.armorBoxMax);

            //if (ReadMemoryValue(addrArmor + armorCNT * 10) == 0x4ffff)
            //{
            //    end = armorCNT;
            //}
            //else
            //{
            //    end = armorCNT;
            //    while (ReadMemoryValue(addrArmor)!= 0x4ffff)
            //    {
            //        end++;
            //        addrArmor += 10;
            //    }
            //}

            for (int i = 1; i < dataDef.armorBoxMax; i++)
            {
                if (ReadMemoryValue(addrArmor) != 0x4ffff)
                {
                    WriteMemory(addrArmor, 0xffff);
                    WriteMemory(addrArmor + 2, 4);
                    WriteMemory(addrArmor + 4, 0);
                    WriteMemory(addrArmor + 6, 0);
                    WriteMemory(addrArmor + 8, 0);
                    addrArmor += 10;
                }
                else
                {
                    break;
                }
            }
            armorCNT = 0;
        }

        private int[] delLastArmor()
        {
            long addrArmor = baseAddr + 0xEA7C;
            int[] res = new int[2];
            if (ReadMemoryValue(addrArmor) != 0x4ffff)
            {
                if ((ReadMemoryValue(addrArmor + armorCNT * 10) == 0x4ffff) && (ReadMemoryValue(addrArmor + armorCNT * 10 - 10) != 0x4ffff))
                {
                    addrArmor = addrArmor + armorCNT * 10;
                }
                else
                {
                    armorCNT = 0;
                    buttonsDisable(0);
                    while (ReadMemoryValue(addrArmor) != 0x4ffff)
                    {
                        addrArmor += 10;
                        armorCNT++;
                    }
                    buttonsEnable(0);
                }
            }
            else
            {
                armorCNT = 0;
            }

            addrArmor -= 10;
            res[0] = (int)(ReadMemoryValue(addrArmor) & 0xffff);
            res[1] = (int)(ReadMemoryValue(addrArmor + 2) & 0x00ff);
            WriteMemory(addrArmor, 0xffff);
            WriteMemory(addrArmor + 2, 4);
            WriteMemory(addrArmor + 4, 0);
            WriteMemory(addrArmor + 6, 0);
            WriteMemory(addrArmor + 8, 0);
            addrArmor = 0;
            armorCNT--;
            return res;
        }

        private void addMitamaToBattle(int start, int sum)
        {
            long addrMitamaCNT = baseAddrBattle + 0x47;
            long addrMitama = baseAddrBattle + 0x11D0;

            WriteMemory(addrMitamaCNT, sum);
            for (int cnt = 0; cnt < sum; cnt++)
            {
                WriteMemory(addrMitama + 4 * cnt, start + cnt);
                WriteMemory(addrMitama + 4 * cnt + 2, 0);
            }
            addrMitama = 0;
            addrMitamaCNT = 0;
        }

        private void unlockWeaponSpecialist()
        {
            labelInfo.Text = "正在处理解锁\"一意专心\"。。。\r\n时间有点长，请耐心等候";
            labelInfo.Update();
            //boxMax();
            addAllItemGet();
            long addrWeapon = baseAddr + 0xA108;
            long addrWeaponGet = baseAddr + 0xd308;

            //while ((ReadMemoryValue(addrWeapon) & 0xffff) != 0xffff)
            //{
            //    addrWeapon += 16;
            //}

            // 0 到 169 是太刀的编号
            for (int i = 0; i < 169; i++)
            {
                //WriteMemory(addrWeapon, i);
                //WriteMemory(addrWeapon + 10, 9);
                //解锁核心，刷新武器的入手代码
                WriteMemory(addrWeaponGet + 3 * i, 0x11);
            }

            labelInfo.Text = "处理完成！\r\n请马上去铁匠铺锻造一把大太刀然后去领取成就！";
            labelInfo.Update();
        }

        private void btnBoxMax_Click(object sender, EventArgs e)
        {
            btnBoxMax.Enabled = false;
            boxMax();
            labelInfo.Text = "素材容量：" + dataDef.itemBoxMax + "\r\n武器容量：" + dataDef.weaponBoxMax + "\t   防具容量：" + dataDef.armorBoxMax;
            labelInfo.Update();
            btnBoxMax.Enabled = true;
        }

        private void btnBoxReduce_Click(object sender, EventArgs e)
        {
            btnBoxReduce.Enabled = false;
            int[] boxSize = boxReduce(200);
            labelInfo.Text = "素材容量：" + boxSize[0] + "\r\n武器容量：" + boxSize[1] + "\t   防具容量：" + boxSize[2];
            labelInfo.Update();
            btnBoxReduce.Enabled = true;
        }

        private void btnAddAllItem_Click(object sender, EventArgs e)
        {
            buttonsDisable(0);
            labelInfo.Text = "正在努力修改中，请稍候。。。";
            labelInfo.Update();
            addAllItem();
            labelInfo.Text = "素材箱现共有" + dataDef.mitamaList.Length + "种素材，每种各78个";
            labelInfo.Update();
            buttonsEnable(0);
        }

        private void btnAllItemGet_Click(object sender, EventArgs e)
        {
            labelInfo.Text = "努力修改中，请稍候";
            labelInfo.Update();
            addAllItemGet();
            labelInfo.Text = "不添加任何素材\r\n铁匠铺全解锁";
            labelInfo.Update();
        }

        private void btnAddAllMitama_Click(object sender, EventArgs e)
        {
            buttonsDisable(0);
            labelInfo.Text = "正在努力修改中，请稍候。。。";
            labelInfo.Update();
            addMitamaRange(0, dataDef.mitamaList.Length);
            //addMitamaRange(0, 300);
            labelInfo.Text = "所有御魂都可以使用，等级为 極\r\n此方式无法获得全御魂收集成就！";
            labelInfo.Update();
            buttonsEnable(0);
        }

        private void btnAddOneMitama_Click(object sender, EventArgs e)
        {
            int id = comboBoxMitama.SelectedIndex;
            labelInfo.Text = "正在努力添加魂：" + id;
            labelInfo.Update();
            addMitamaRange(id, 1);
            labelInfo.Text = comboBoxMitama.SelectedItem + "\r\n添加成功";
            labelInfo.Update();
            if (comboBoxMitama.SelectedIndex == dataDef.mitamaMaxID)
            {
                comboBoxMitama.SelectedIndex = 0;
            }
            else
            {
                comboBoxMitama.SelectedIndex += 1;
            }
        }

        private void btnGetMitamaOnWeapon_Click(object sender, EventArgs e)
        {
            int id1, id2, id3;
            long addrWeapon = baseAddr + 0xA108;
            long addrEquipedIndex = baseAddr + 0x7708;

            int equipedIndex = (int)(ReadMemoryValue(addrEquipedIndex) & 0xffff);
            //for (int i = 0; i < equipedIndex; i++)
            //{
            //    addrWeapon += 16;
            //}
            addrWeapon += 16 * equipedIndex;
            id1 = (int)(ReadMemoryValue(addrWeapon + 2) & 0xffff);
            id2 = (int)(ReadMemoryValue(addrWeapon + 4) & 0xffff);
            id3 = (int)(ReadMemoryValue(addrWeapon + 6) & 0xffff);


            mitamaIDOnWeapon1.Value = (long)((id1 == 0xFFFF) ? -1 : id1);
            mitamaIDOnWeapon2.Value = (long)((id2 == 0xFFFF) ? -1 : id2);
            mitamaIDOnWeapon3.Value = (long)((id3 == 0xFFFF) ? -1 : id3);

            addrWeapon = 0;
            addrEquipedIndex = 0;
        }
        private void btnModifyMitamaOnWeapon_Click(object sender, EventArgs e)
        {
            int id1, id2, id3;
            bool flag1 = true;
            bool flag2 = true;
            bool flag3 = true;
            id1 = (int)mitamaIDOnWeapon1.Value;
            id2 = (int)mitamaIDOnWeapon2.Value;
            id3 = (int)mitamaIDOnWeapon3.Value;

            if (id1 == id2)
            {
                flag1 = (id1 + id2 == -2) ? true : false;
            }
            if (id2 == id3)
            {
                flag2 = (id2 + id3 == -2) ? true : false;
            }
            if (id1 == id3)
            {
                flag3 = (id1 + id3 == -2) ? true : false;
            }
            if (!(flag1 && flag2 && flag3))
            {
                MessageBox.Show("魂编号必须不一样！");
                labelInfo.Text = "魂编号必须不一样！";
                labelInfo.Update();
                return;
            }
            else
            {
                // 添加魂先
                if (id1 == -1)
                {
                    id1 = 0xffff;
                }
                else
                {
                    addMitamaRange(id1, 1);
                }
                if (id2 == -1)
                {
                    id2 = 0xffff;
                }
                else
                {
                    addMitamaRange(id2, 1);
                }
                if (id3 == -1)
                {
                    id3 = 0xffff;
                }
                else
                {
                    addMitamaRange(id3, 1);
                }

                modifyMitamaOnWeapon(id1, id2, id3);
            }
            labelInfo.Text = "武器配魂已经修改！";
            labelInfo.Update();
        }

        private void btnAddWeapon_Click(object sender, EventArgs e)
        {
            int id = (int)weaponIDBox.Value;
            if (id > 1664)
            {
                if (id < 1800)
                {
                    weaponIDBox.Value = 1800;
                    return;
                }
                else
                {
                    if ((id - 1800) % 22 < 4)
                    {
                        addOneWeapon(id);
                        labelInfo.Text = "代码 " + id + " 的武器已经添加";
                        labelInfo.Update();
                        if (((id - 1800) % 22 == 3) && id < dataDef.weaponMaxID)
                        {
                            weaponIDBox.Value += 19;
                        }
                        else
                        {
                            if (id == dataDef.weaponMaxID)
                            {
                                weaponIDBox.Value = 0;
                            }
                            else
                            {
                                weaponIDBox.Value += 1;
                            }
                        }
                        return;
                    }
                    labelInfo.Text = "代码 " + id + " 的武器不存在";
                    labelInfo.Update();
                    weaponIDBox.Value += 1;
                    return;
                }
            }
            if (!dataDef.weaponNone.Contains(id))
            {
                addOneWeapon(id);
                labelInfo.Text = "代码 " + id + " 的武器已经添加";
                labelInfo.Update();
            }
            else
            {
                labelInfo.Text = "代码 " + id + " 的武器不存在";
                labelInfo.Update();
            }
            if (id == dataDef.weaponMaxID)
            {
                weaponIDBox.Value = dataDef.weaponMinID;
                return;
            }
            weaponIDBox.Value += 1;
        }

        private void btnDelWeapon_Click(object sender, EventArgs e)
        {
            int res = delLastWeapon();
            if (res != -1)
            {
                labelInfo.Text = "代码 " + res + " 的武器已经删除";
            }
            else
            {
                labelInfo.Text = "不能删除最后一把武器";
            }
            labelInfo.Update();
        }

        private void btnDelAllWeapon_Click(object sender, EventArgs e)
        {
            buttonsDisable(0);
            labelInfo.Text = "正在删除所有的武器！请稍候。。。";
            labelInfo.Update();
            delAllWeapon();
            labelInfo.Text = "删除完成，现在仅有手中一把武器";
            labelInfo.Update();
            buttonsEnable(0);
        }

        private void btnAddArmor_Click(object sender, EventArgs e)
        {
            int id = (int)armorIDBox.Value;
            if (dataDef.armorNone.Contains(id))
            {
                labelInfo.Text = "主代码 " + id + " 的防具不存在";
                labelInfo.Update();
                armorIDBox.Value += 1;
                return;
            }
            else if (dataDef.armorAmulet.Contains(id))
            {
                addArmor(id, 0);
                labelInfo.Text = "主代码 " + id + " 的头盔已添加";
                labelInfo.Update();
                armorIDBox.Value += 1;
                return;
            }
            else if (dataDef.armorBody.Contains(id))
            {
                addArmor(id, 1);
                labelInfo.Text = "主代码 " + id + " 的服饰已添加";
                labelInfo.Update();
                armorIDBox.Value += 1;
                return;
            }
            else if (dataDef.armorGlove.Contains(id))
            {
                addArmor(id, 2);
                labelInfo.Text = "主代码 " + id + " 的手套已添加";
                labelInfo.Update();
                armorIDBox.Value += 1;
                return;
            }
            else if (dataDef.armorShoe.Contains(id))
            {
                addArmor(id, 3);
                labelInfo.Text = "主代码 " + id + " 的鞋已添加";
                labelInfo.Update();
                armorIDBox.Value += 1;
                return;
            }
            else
            {
                addArmor(id, 4);
                labelInfo.Text = "主代码 " + id + " 的套装已添加";
                labelInfo.Update();

                // 最后一位是套装！
                if (id == dataDef.armorMaxID)
                {
                    armorIDBox.Value = dataDef.armorMinID;
                    return;
                }
                else
                {
                    armorIDBox.Value += 1;
                }
            }
        }

        private void btnDelArmor_Click(object sender, EventArgs e)
        {
            int[] res = delLastArmor();
            labelInfo.Text = "代码 " + res[0] + " 的 " + dataDef.armorType[res[1]] + " 已经删除";
            labelInfo.Update();
        }

        private void btnDelAllArmor_Click(object sender, EventArgs e)
        {
            buttonsDisable(0);
            labelInfo.Text = "正在删除所有的防具！请稍候。。。";
            labelInfo.Update();
            delAllArmor();
            labelInfo.Text = "删除完成，你现在是裸体";
            labelInfo.Update();
            buttonsEnable(0);
        }

        private void btnAddOneItem_Click(object sender, EventArgs e)
        {
            //buttonsDisable(0);
            int id = (int)itemIDBox.Value;
            if (dataDef.materialID.Contains(id))
            {
                addOneItem(id);
                labelInfo.Text = "代码 " + id + " 的素材已经添加78个";
                labelInfo.Update();
            }
            else
            {
                labelInfo.Text = "代码 " + id + " 的素材不存在";
                labelInfo.Update();
            }
            if (id == dataDef.materialMaxID)
            {
                itemIDBox.Value = dataDef.materialMinID;
            }
            else
            {
                itemIDBox.Value += 1;
            }
            //buttonsEnable(0);
        }

        private void btnDelAllMitata_Click(object sender, EventArgs e)
        {

            labelInfo.Text = "正在删除所有御魂。。。\r\n只会留下第一个“赖元光”";
            labelInfo.Update();
            delAllMitama();
            labelInfo.Text = "删除完成，开始刷成就吧！";
            labelInfo.Update();
            btnDelAllMitata.Visible = false;
            btnMitamaOnBattle1.Visible = true;
            btnMitamaOnBattle2.Visible = true;
            btnMitamaOnBattle3.Visible = true;
            btnMitamaOnBattle4.Visible = true;
            btnMitamaOnBattle5.Visible = true;
            btnMitamaOnBattle6.Visible = true;
            btnMitamaOnBattle7.Visible = true;
            btnMitamaCNT = 7;
        }
        private void btnMitamaOnBattle1_Click(object sender, EventArgs e)
        {
            addMitamaToBattle(0, 50);
            labelInfo.Text = "结算画面会有50个魂入手。。。\r\n但是你是看不完的，做一个安静的美男子吧。。。";
            labelInfo.Update();
            btnMitamaOnBattle1.Visible = false;
            btnMitamaCNT--;
            if (btnMitamaCNT == 0)
            {
                btnDelAllMitata.Visible = true;
            }
        }

        private void btnMitamaOnBattle2_Click(object sender, EventArgs e)
        {
            addMitamaToBattle(50, 50);
            labelInfo.Text = "结算画面会有50个魂入手。。。\r\n但是你是看不完的，做一个安静的美男子吧。。。";
            labelInfo.Update();
            btnMitamaOnBattle2.Visible = false;
            btnMitamaCNT--;
            if (btnMitamaCNT == 0)
            {
                btnDelAllMitata.Visible = true;
            }
        }

        private void btnMitamaOnBattle3_Click(object sender, EventArgs e)
        {
            addMitamaToBattle(100, 50);
            labelInfo.Text = "结算画面会有50个魂入手。。。\r\n但是你是看不完的，做一个安静的美男子吧。。。";
            labelInfo.Update();
            btnMitamaOnBattle3.Visible = false;
            btnMitamaCNT--;
            if (btnMitamaCNT == 0)
            {
                btnDelAllMitata.Visible = true;
            }
        }

        private void btnMitamaOnBattle4_Click(object sender, EventArgs e)
        {
            addMitamaToBattle(150, 50);
            labelInfo.Text = "结算画面会有50个魂入手。。。\r\n但是你是看不完的，做一个安静的美男子吧。。。";
            labelInfo.Update();
            btnMitamaOnBattle4.Visible = false;
            btnMitamaCNT--;
            if (btnMitamaCNT == 0)
            {
                btnDelAllMitata.Visible = true;
            }
        }

        private void btnMitamaOnBattle5_Click(object sender, EventArgs e)
        {
            addMitamaToBattle(200, 50);
            labelInfo.Text = "结算画面会有50个魂入手。。。\r\n但是你是看不完的，做一个安静的美男子吧。。。";
            labelInfo.Update();
            btnMitamaOnBattle5.Visible = false;
            btnMitamaCNT--;
            if (btnMitamaCNT == 0)
            {
                btnDelAllMitata.Visible = true;
            }
        }

        private void btnMitamaOnBattle6_Click(object sender, EventArgs e)
        {
            addMitamaToBattle(250, 50);
            labelInfo.Text = "结算画面会有50个魂入手。。。\r\n但是你是看不完的，做一个安静的美男子吧。。。";
            labelInfo.Update();
            btnMitamaOnBattle6.Visible = false;
            btnMitamaCNT--;
            if (btnMitamaCNT == 0)
            {
                btnDelAllMitata.Visible = true;
            }
        }

        private void btnMitamaOnBattle7_Click(object sender, EventArgs e)
        {
            addMitamaToBattle(300, dataDef.mitamaMaxID - 299);
            labelInfo.Text = "结算画面会有13个魂入手。。。\r\n个别魂入手时有中文语音哦！";
            labelInfo.Update();
            btnMitamaOnBattle7.Visible = false;
            btnMitamaCNT--;
            if (btnMitamaCNT == 0)
            {
                btnDelAllMitata.Visible = true;
            }
        }

        private void btnShowDLCcode_Click(object sender, EventArgs e)
        {
            MessageBox.Show("DLC 武器代码从 1800 到 1979，程序会自动跳过空代码。" +
                "\r\nDLC 衣服代码为 204 到 207，238 到 243。");
        }

        private void btnUnlockWeaponSpecialist_Click(object sender, EventArgs e)
        {
            buttonsDisable(0);
            unlockWeaponSpecialist();
            weaponIDBox.Value = 180;
            buttonsEnable(0);
        }

        private void btnResetRegister_Click(object sender, EventArgs e)
        {
            long addrRegister = baseAddr + 0x7718;
            for (int i = 0; i < 160; i++)
            {
                WriteMemory(addrRegister + i * 2, 0xffff);
            }
            labelInfo.Text = "所有登录数据已经清空\r\n可以正常打开登录装备菜单";
            labelInfo.Update();
        }
    }
}
